package com.sevencar.peterview;

import java.util.ArrayList;
import java.util.List;

import com.sevencar.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MyFragment extends Fragment {
    private static final String KEY = "key_data_list";

    private List<ImageData> dataList;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private DataAdapter<ImageData> adapter;

    public static Fragment newInstance(ArrayList<ImageData> list) {
        Fragment f = new MyFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(KEY, list);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataList = getArguments().getParcelableArrayList(KEY);
        adapter = new DataAdapter<ImageData>(dataList, R.layout.item, R.id.imageView, R.id.textView, R.drawable.empty_photo, getResources());
        adapter.setOnClickListener(new DataAdapter.OnClickListener<ImageData>() {
            @Override
            public void onClick(ImageData data) {
            	Toast.makeText(getActivity(), data.getTitle(), Toast.LENGTH_SHORT).show(); 
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.recyclerview, container, false);
        recyclerView = (RecyclerView) result.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        int spanCount = 1; // 只显示一行
        layoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        return result;
    }
}
